package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.UserOrderDispatch;
import com.happy.cat.project.system.service.IUserOrderDispatchService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 派单记录Controller
 *
 * @author verne
 * @date 2020-08-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userOrderDispatch" )
@Api("派单记录 API")
public class UserOrderDispatchController extends BaseController {

    private final IUserOrderDispatchService iUserOrderDispatchService;

    /**
     * 查询派单记录列表
     */
    @ApiOperation(value = "查询派单记录列表",notes = "查询派单记录列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserOrderDispatch userOrderDispatch)
    {
        startPage();
        LambdaQueryWrapper<UserOrderDispatch> lqw = new LambdaQueryWrapper<UserOrderDispatch>();
        if (userOrderDispatch.getUserId() != null){
            lqw.eq(UserOrderDispatch::getUserId ,userOrderDispatch.getUserId());
        }
        if (StringUtils.isNotBlank(userOrderDispatch.getOrderCode())){
            lqw.eq(UserOrderDispatch::getOrderCode ,userOrderDispatch.getOrderCode());
        }
        if (StringUtils.isNotBlank(userOrderDispatch.getCode())){
            lqw.eq(UserOrderDispatch::getCode ,userOrderDispatch.getCode());
        }
        if (StringUtils.isNotBlank(userOrderDispatch.getChatroomCode())){
            lqw.eq(UserOrderDispatch::getChatroomCode ,userOrderDispatch.getChatroomCode());
        }
        if (StringUtils.isNotBlank(userOrderDispatch.getChatroomType())){
            lqw.eq(UserOrderDispatch::getChatroomType ,userOrderDispatch.getChatroomType());
        }
        if (userOrderDispatch.getTeamCategoryItemId() != null){
            lqw.eq(UserOrderDispatch::getTeamCategoryItemId ,userOrderDispatch.getTeamCategoryItemId());
        }
        if (userOrderDispatch.getPrice() != null){
            lqw.eq(UserOrderDispatch::getPrice ,userOrderDispatch.getPrice());
        }
        if (userOrderDispatch.getSkipOrders() != null){
            lqw.eq(UserOrderDispatch::getSkipOrders ,userOrderDispatch.getSkipOrders());
        }
        if (userOrderDispatch.getStatus() != null){
            lqw.eq(UserOrderDispatch::getStatus ,userOrderDispatch.getStatus());
        }
        List<UserOrderDispatch> list = iUserOrderDispatchService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出派单记录列表
     */
    @ApiOperation(value = "导出派单记录列表",notes = "导出派单记录列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:export')" )
    @Log(title = "派单记录" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserOrderDispatch userOrderDispatch) {
        LambdaQueryWrapper<UserOrderDispatch> lqw = new LambdaQueryWrapper<UserOrderDispatch>(userOrderDispatch);
        List<UserOrderDispatch> list = iUserOrderDispatchService.list(lqw);
        ExcelUtil<UserOrderDispatch> util = new ExcelUtil<UserOrderDispatch>(UserOrderDispatch. class);
        return util.exportExcel(list, "userOrderDispatch" );
    }

    /**
     * 获取派单记录详细信息
     */
    @ApiOperation(value = "获取派单记录详细信息",notes = "获取派单记录详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserOrderDispatchService.getById(id));
    }

    /**
     * 新增派单记录
     */
    @ApiOperation(value = "新增派单记录",notes = "新增派单记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:add')" )
    @Log(title = "派单记录" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserOrderDispatch userOrderDispatch) {
        return toAjax(iUserOrderDispatchService.save(userOrderDispatch) ? 1 : 0);
    }

    /**
     * 修改派单记录
     */
    @ApiOperation(value = "修改派单记录",notes = "修改派单记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:edit')" )
    @Log(title = "派单记录" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserOrderDispatch userOrderDispatch) {
        return toAjax(iUserOrderDispatchService.updateById(userOrderDispatch) ? 1 : 0);
    }

    /**
     * 删除派单记录
     */
    @ApiOperation(value = "删除派单记录",notes = "删除派单记录",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userOrderDispatch:remove')" )
    @Log(title = "派单记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserOrderDispatchService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
